package com.workflow.workflowproject.until;


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson2.JSONObject;
import com.mybatisflex.core.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

public class ElementToVant {
    /**
     * element ui 的 formcreate 转 vant 组件
     * @param pcFieldList
     * @param
     * @return
     */
    public static List<JSONObject> elementToVant(List<JSONObject> pcFieldList){
        List<JSONObject> mvFieldList = new ArrayList<>();
        for(JSONObject jsonObject : pcFieldList){
            //深拷贝elementui的组件
            JSONObject jsonObjectMv = jsonObject.clone();
            //将elementui的组件类型转化为vant的组件类型
            String vantType = FieldEnum.getVantTypeByElementType(jsonObjectMv.getString("type"));
            if(StringUtil.isNotBlank(vantType)){
                jsonObjectMv.put("type",FieldEnum.getVantTypeByElementType(jsonObjectMv.getString("type")));
            }
            //在vant的props中，将组件提高为vant
            jsonObjectMv.put("label-align","top");
            jsonObjectMv.put("labelAlign","top");
            JSONObject jsonObjectWrap = new JSONObject();
            jsonObjectWrap.put("labelAlign","top");
            jsonObjectMv.put("wrap",jsonObjectWrap);
            if(ObjectUtils.isEmpty(jsonObjectMv.get("props"))){
                JSONObject options = new JSONObject();
                options.put("label-align","top");
                options.put("labelAlign","top");
                jsonObjectMv.put("props",options);
            }else{
                LinkedHashMap<String,Object> props = (LinkedHashMap<String,Object>) jsonObject.get("props");
                props.put("label-align","top");
                props.put("labelAlign","top");
                jsonObjectMv.put("props",props);
            }
            //需要转化的数据
            if(FieldEnum.getOptionByElementType(jsonObjectMv.getString("type")).compareTo(1) == 0){
                List<LinkedHashMap<String,Object>> jsonObjects = (List<LinkedHashMap<String, Object>>) jsonObjectMv.get("options");
                List<JSONObject> mvJsonObjects = new ArrayList<>();
                for(LinkedHashMap<String,Object> jsonObjectData : jsonObjects){
                    JSONObject mvJson = new JSONObject();
                    mvJson.put("text",jsonObjectData.get("label"));
                    mvJson.put("value",jsonObjectData.get("value"));
                    mvJsonObjects.add(mvJson);
                }
                LinkedHashMap<String,Object> props = (LinkedHashMap<String,Object>) jsonObjectMv.get("props");
                props.put("options",mvJsonObjects);
                props.put("label-align","top");
                props.put("labelAlign","top");
                jsonObjectMv.put("props",props);
            }
            mvFieldList.add(jsonObjectMv);
        }
        return mvFieldList;
    }

}
